package com.dao.systemDao;

import java.util.ArrayList;
import java.util.HashMap;

import admin.mode.AdminMode;
import base.Power;

public class UsersDao implements IUsersDao {
	/**
	 * @author lcl
	 * @param 查询所有的用户
	 * **/
	public ArrayList<HashMap<String, String>> userList(Power power)
			throws Exception {
		ArrayList<HashMap<String, String>> users = power
				.getListFromSql("select * from `users` where if_default=0");
		return users;
	}

	public boolean delUser(Power power, String id) {
		try {
			power.executeUpdate("delete from `users` where (`role_id`='" + id
					+ "')");
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean groupDelete(Power power, AdminMode admin) throws Exception {
		String[] ids = power.getRequest().getParameterValues("ids");
		String str = base.tools.Tools.getStringFromStrings(ids, ",");
		String sql = "delete from `users` where role_id in(" + str + ")";
		if (power.executeUpdate(sql) > -1) {
			for (int i = 0; i < ids.length; i++) {
				delUser(power, ids[i]);
			}
			return true;
		}
		return false;
	}

	public boolean updataUser(Power power, String id) throws Exception {
		String[] ids = power.getRequest().getParameterValues("ids");
		power.executeUpdate("update `user_top_menu` set"
				+ " `if_show`='false' where `users_id`=" + id);
		power.executeUpdate("update `users` set "
				+ " `is_used`='1' where (`role_id`='" + id + "')");
		for (int i = 0; i < ids.length; i++) {
			String sql = "update `user_top_menu` set `if_show`='true' where `top_menu_id_users`='"
					+ ids[i] + "' and `users_id`=" + id;
			power.executeUpdate(sql);

		}
		return true;
	}

	public ArrayList<HashMap<String, String>> userInfo(Power power, String id)
			throws Exception {
		ArrayList<HashMap<String, String>> users = power
				.getListFromSql("select * from `user_top_menu` t1,`top_menu` t2  where "
						+ "t1.top_menu_id_users = t2.top_menu_id  and t1.users_id="
						+ id);
		return users;
	}

	public boolean insertUser(Power power, String username, String password) {
		String sql = "insert into `users` (`username`, `password`) values ('"
				+ username + "', '" + password + "')";
		String id = "";
		try {
			power.executeUpdate(sql);
			id = base.tools.Tools.getLastInsertID(power);

			ArrayList<HashMap<String, String>> list2 = power
					.getListFromSql("select * from `top_menu`");
			for (int i = 0; i < list2.size(); i++) {
				String ids = list2.get(i).get("TOP_MENU_ID");
				power
						.executeUpdate("insert into `user_top_menu` "
								+ " (`top_menu_id_users`, `users_id`,`if_show`) values ('"
								+ ids + "', '" + id + "','false')");
			}
		} catch (Exception e) {
			try {
				rollback(power, id);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return true;
	}

	private void rollback(Power power, String id) throws Exception {
		power.executeUpdate("delete from `users` where (`role_id`='" + id
				+ "')");
	}

	public boolean updataPassword(Power power) {
		return false;
	}
}
